# -*- coding: utf-8 -*-

import  wx
import  os

import xlrd
import sys

import  wx.wizard as wiz
#import  images
import  encodings.mbcs
import  codecs
from encodings import gbk

import wx.grid
import selector
import xlsgrid
import colmapping
import newcolumns
import mappicker
import traceback
import wizdata

class Excel:
  # read data from xls file and determine the start row and col
  
  def __init__(self, filename):
    #self.filename = filename
    self.data = ()
    self.collabel = ()
    self.rowlabel = ()
    self.partdata = ()
    wdata = wizdata.WizData()
    
    #print filename
    self.book = xlrd.open_workbook(filename)
    self.datemode = self.book.datemode
    #print self.datemode
    self.sheets = self.book.sheets()
    self.currentsheet = self.sheets[0]
    #wdata = wizdata.WizData()
    
    self.ncols = self.currentsheet.ncols
    self.nrows = self.currentsheet.nrows
    
    self.rowstart = None
    self.colstart = None
    
    for x in range(10):
      colstart = None
      colcount = 0
      for c in range(self.ncols):
        #print "%s, %s"%(x,c),
        val = self.currentsheet.cell_value(x,c)
        if  type(val) == unicode:
          item = val.encode("gbk")   # as a parameter; by mjj
        else:
          item = val
        #print type(cell)
        if item ==  "" and colstart is None:
          #print 'none'
          colstart = c + 1
          #print colstart,
        if item != "":
          colcount = colcount +1
          #print colcount-colstart, item
        else:
          pass
          #print val
        #if c ==6:
        #  break
        #  #print item
      #print x
      if  colcount + colstart == self.ncols : 
        #print "label row:%s, col start:%s"%(x,colstart)
        self.rowstart = x
        self.colstart = colstart

        break
        
    self.__getData()


    
    self.colLabelRange = ((3,1),(3,20))
    self.rowLabelRange = range(4,41)
    self.rowLabelSeg1 = range(4,8)
    self.rowLabelSeg2 = range(36,41)
    self.setExcelCol()
    
    
  def setExcelCol(self):
    data = wizdata.WizData()
    rowstart = self.rowstart
    colstart = self.colstart
    colend = self.ncols
    labels = []
    for i in range(colstart, colend):
      val =  self.currentsheet.cell_value(rowstart,i)
      labels.append(val)
      #print val.encode('utf8')
    data.labels = labels
    
  """
  def getColLabel(self):
    start, end = self.colLabelRange
    print start[0], start[1]
    print end[0], end[1]
  """
  """
  def getRowLabel(self):
    #for x in self.rowLabelRange:
    for x in self.rowLabelSeg1:
      print x,
    print
  """  
  def getLabelSeg(self):
    print self.rowLabelSeg1
    print "..."
    print self.rowLabelSeg2
  
  def getRowLabels(self):
    cols = []
    for x in range(self.nrows):
      cols.append(str(x))
    return cols

  def getColLabels(self):
    cols = []
    for x in range(self.ncols):
      cols.append(str(x))
    return cols

  def getAllData(self):
    return self.alldata
  
  def __getData(self):
    sheet = self.currentsheet

    #print cols, rows
    data = tuple()
    all = []
    onerowlabel =[]
    for i in xrange(2,self.ncols):
      val =  sheet.cell_value(2,i)
      item = self.getValue(val,2,i)
      onerowlabel.append(item)
    self.onelinelabel = tuple(onerowlabel)
    #print self.onelinelabel
      
    for r in xrange(0,self.nrows):#self.rowstart
      
      #print tuple(onerow)
      """
      for x in onerow:
          print type(x),
      print
      """
      #print type(onerow)
      oneline = []
      
      for c in xrange(0,self.ncols):#self.colstart
        val = sheet.cell_value(r,c)
        #print ("(%s,%s) :") %(r,c),
        item = self.getValue(val,r,c)
        oneline.append(item)
        #print item,
        #print
      all.append(tuple(oneline))
    data = tuple(oneline)
    self.alldata = tuple(all)
    #return self.alldata
    """
    for x in alldata:
      for y in x:
        print y,
      print
    #all.append(data)
    """
    #print all

  def getValue(self, val, r, c):
    if  type(val) == unicode:
      #item = val.encode("gbk")
      item = val
      
    elif self.currentsheet.cell_type(r,c) == xlrd.XL_CELL_DATE:
      thisdate = xlrd.xldate_as_tuple(val, self.datemode)
      item = "%s-%s-%s" %(thisdate[0],thisdate[1], thisdate[2])
    else:
      item =  val
    return item
    
      
def test():
    data = wizdata.WizData()
    data.filename = "../salary1.xls"
    xls = Excel(data.filename) 
    for x in  data.labels:
      print x.encode('utf8')
    #xls.getData()
    #data = wizdata.WizData()
    #print data.rowstart
    #print data.colstart
    #xls.getColLabel()
    #xls.getRowLabel()
    #xls.getLabelSeg()


if __name__ == "__main__":
    test()
